use std::sync::Arc;

use crate::error::*;
use serde::{Deserialize, Serialize};

pub static DEFAULT_CONNECTOR_CIPHER_LIST: &str =
    "DEFAULT:!aNULL:!eNULL:!MD5:!3DES:!DES:!RC4:!IDEA:!SEED:!aDSS:!SRP:!PSK";

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct SslConnectorConf {
    pub cipher_list: Option<String>,
}

#[allow(dead_code)]
impl SslConnectorConf {
    pub fn to_runtime_conf(
        &self,
        pre_conf: Option<&Arc<SslConnectorRuntimeConf>>,
    ) -> Result<Arc<SslConnectorRuntimeConf>> {
        let def_cipher_list = match pre_conf {
            Some(pre_conf) => pre_conf.cipher_list.clone(),
            None => "".to_string(),
        };

        let cipher_list = self.cipher_list.clone().unwrap_or(def_cipher_list);
        let runtime_conf = SslConnectorRuntimeConf { cipher_list };

        Ok(Arc::new(runtime_conf.valid()?))
    }
}

pub struct SslConnectorRuntimeConf {
    pub cipher_list: String,
}

impl SslConnectorRuntimeConf {
    pub fn valid(mut self) -> Result<Self> {
        if self.cipher_list.is_empty() {
            self.cipher_list = DEFAULT_CONNECTOR_CIPHER_LIST.to_string();
        }

        Ok(self)
    }
}
